<div id="optPage" style="background-color: #FFFFFF">
    <form class="layui-form layui-form-pane" id="commentForm">
        <div class="main-container" style="padding-top: 10px!important;padding-bottom: 30px!important;">
            <div class="block-generic">

                <div class="block-first">
                    <div class="layui-form-item">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-md12">
                                <label class="layui-form-label">预警类型</label>
                                <div class="layui-input-block">
                                    <input type="hidden" id="id" name="id">
                                    <input type="text" id="title" readonly  autocomplete="off" class="layui-input disabled">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-md12">
                                <label class="layui-form-label"><text>*</text>预警期2</label>
                                <div class="layui-input-block">
                                    <span class="curwen zu_1 zu_1_zi">自到期前</span>
                                    <input class="layui-input curinput zu_1 zu_1_shu" type="number" name="yuone" id="yuone" placeholder="请输入" clearable autocomplete="off">
                                    <span class="curdan zu_1 zu_1_dan">天</span>
                                    <span class="curwen zu_2 zu_2_zi">至到期后</span>
                                    <input class="layui-input curinput zu_2 zu_2_shu" type="number" name="yutwo" id="yutwo" placeholder="请输入" clearable autocomplete="off">
                                    <span class="curdan zu_2 zu_2_dan">天</span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item" id="quenDiv">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-md12">
                                <label class="layui-form-label"><text>*</text> 预警频率</label>
                                <div class="layui-input-block radio-block" id="quen"></div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide" id="quenweekDiv">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-md12">
                                <label class="layui-form-label"><text>*</text>预警日期</label>
                                <div class="layui-input-block radio-block" id="quenweek" style="height: auto"></div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide" id="quendateDiv">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-md12">
                                <label class="layui-form-label" style="height: 92px"><text>*</text>预警日期</label>
                                <div class="layui-input-block radio-block" id="quendate" style="height: auto!important;"></div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-md12">
                                <label class="layui-form-label"><text>*</text>预警对象</label>
                                <div class="layui-input-block radio-block" id="duitemp" style="height: auto"></div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide" id="duierDiv">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-md12">
                                <label class="layui-form-label"><text>*</text>指定人员</label>
                                <div class="layui-input-block">
                                    <div class="tanxian">
                                        <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=zhier&type=user&single=2', '770px', '490px');"></div>
                                        <div class="tanxiannei" id="zhier"></div>
                                    </div>
                                    <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=zhier&type=user&single=2', '770px', '490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                                    <div id="zhier_tanerror" class="tanerrormsg"></div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-md12">
                                <label class="layui-form-label"><text>*</text>预警状态</label>
                                <div class="radio-block" style="height: auto">
                                    <input type="checkbox" id="state" lay-skin="switch" name="state" lay-text="启用|停用" class="layui-input">
                                    <div style="padding-left: 140px;margin: 10px 0">说明：启用状态下，根据预警规则自动触发系统预警，发送系统内的消息通知；停用则不会触发预警通知</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-md12">
                                <label class="layui-form-label">最后修改人</label>
                                <div class="layui-input-block">
                                    <input type="text" id="updated_user_name" readonly autocomplete="off" class="layui-input disabled">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-md12">
                                <label class="layui-form-label">最后修改时间</label>
                                <div class="layui-input-block">
                                    <input type="text" id="updated_at" readonly autocomplete="off" class="layui-input disabled">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="page-title-opt-list" style="text-align: center">
                    <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">提交</button>
                </div>
            </div>
        </div>
    </form>
</div>
<script>
    document.getElementById('yuone').addEventListener('input', function() {
        var thisValue = $(this).val() || 0;
        if(thisValue < 1){
            thisValue = 0;
        }
        if(thisValue > 999){
            thisValue = 999
        }
        $(this).val(parseInt(thisValue));
    });
    document.getElementById('yutwo').addEventListener('input', function() {
        var thisValue = $(this).val() || 0;
        if(thisValue < 1){
            thisValue = 0;
        }
        if(thisValue > 999){
            thisValue = 999
        }
        $(this).val(parseInt(thisValue));
    });



    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();


        form.on('radio(quen)', function(data){
            if(data.value == 'm'){
                $("#quenweekDiv").addClass('layui-hide');
                $("#quendateDiv").removeClass('layui-hide');
            }else if(data.value == 'w'){
                $("#quenweekDiv").removeClass('layui-hide');
                $("#quendateDiv").addClass('layui-hide');
            }else{
                $("#quenweekDiv").addClass('layui-hide');
                $("#quendateDiv").addClass('layui-hide');
            }
        });

        form.on('checkbox(duier[])', function(data){
            var checkboxes = document.querySelectorAll('input[name="duier[]"]:checked');
            var values = [];
            for (var i = 0; i < checkboxes.length; i++) {
                values.push(checkboxes[i].value);
            }

            if(str_in_array('pointer', values)){
                $("#duierDiv").removeClass('layui-hide');
            }else{
                $("#duierDiv").addClass('layui-hide');
            }
        });


        form.on('submit(add)', function(data){
            data.field.zhier = tan_tags_getvalue("#zhier", 'user') || '';
            facade.ajax({url: ask_warn_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });
    });

    function edit_data(id){
        facade.ajax({type: 'get', url: ask_warn_edit, data:{id: id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;

                $(".yulei").addClass('layui-hide');
                $(".yulei_"+data.yulei).removeClass('layui-hide');

                if(data.yulei == 1){
                    $(".zu_1").removeClass('layui-hide');
                    $(".zu_2").removeClass('layui-hide');
                    $(".zu_1_zi").text('自到期前');
                    $(".zu_1_dan").text('天');
                    $(".zu_2_zi").text('至到期后');
                    $(".zu_2_dan").text('天');
                }
                if(data.yulei == 2){
                    $(".zu_1").removeClass('layui-hide');
                    $(".zu_2").removeClass('layui-hide');
                    $(".zu_1_zi").text('逾期');
                    $(".zu_1_dan").text('天');
                    $(".zu_2_zi").text('至');
                    $(".zu_2_dan").text('天');
                }
                if(data.yulei == 3){
                    $(".zu_1").removeClass('layui-hide');
                    $(".zu_2").addClass('layui-hide');
                    $(".zu_1_zi").text('产值进度（审定产值 / 施工合同金额）＞ 回款进度（回款金额 / 施工合同金额）+');
                    $(".zu_1_dan").text('%');
                }
                if(data.yulei == 4){
                    $(".zu_1").removeClass('layui-hide');
                    $(".zu_2").addClass('layui-hide');
                    $(".zu_1_zi").text('总成本 / 总预算＞');
                    $(".zu_1_dan").text('%');
                }
                if(data.yulei == 5){
                    $(".zu_1").removeClass('layui-hide');
                    $(".zu_2").addClass('layui-hide');
                    $(".zu_1_zi").text('材料成本 / 材料预算＞');
                    $(".zu_1_dan").text('%');
                }
                if(data.yulei == 6){
                    $(".zu_1").removeClass('layui-hide');
                    $(".zu_2").addClass('layui-hide');
                    $(".zu_1_zi").text('人工成本 / 人工预算＞');
                    $(".zu_1_dan").text('%');
                }
                if(data.yulei == 7){
                    $(".zu_1").removeClass('layui-hide');
                    $(".zu_2").addClass('layui-hide');
                    $(".zu_1_zi").text('机械设备成本 / 机械设备预算＞');
                    $(".zu_1_dan").text('%');
                }
                if(data.yulei == 8){
                    $(".zu_1").removeClass('layui-hide');
                    $(".zu_2").addClass('layui-hide');
                    $(".zu_1_zi").text('分包成本 / 分包预算＞');
                    $(".zu_1_dan").text('%');
                }
                if(data.yulei == 9){
                    $(".zu_1").removeClass('layui-hide');
                    $(".zu_2").addClass('layui-hide');
                    $(".zu_1_zi").text('其他成本 / 其他预算＞');
                    $(".zu_1_dan").text('%');
                }


                if(data.havequen < 1){
                    $("#quenDiv").addClass('layui-hide');
                }else{
                    $("#quenDiv").removeClass('layui-hide');
                }


                $("#id").val(data.id);
                $("#title").val(data.title);
                $("#yuone").val(data.yuone);
                $("#yutwo").val(data.yutwo);
                $("#updated_user_name").val(data.updated_user_name);
                $("#updated_at").val(data.updated_at);





                var quen_str = '';
                if(data.quen == 'd'){
                    quen_str += '<input type="radio" name="quen" lay-filter="quen" value="d" title="每日" checked>';
                    quen_str += '<input type="radio" name="quen" lay-filter="quen" value="w" title="每周">';
                    quen_str += '<input type="radio" name="quen" lay-filter="quen" value="m" title="每月">';

                    $("#quenweekDiv").addClass('layui-hide');
                    $("#quendateDiv").addClass('layui-hide');
                }
                if(data.quen == 'w'){
                    quen_str += '<input type="radio" name="quen" lay-filter="quen" value="d" title="每日">';
                    quen_str += '<input type="radio" name="quen" lay-filter="quen" value="w" title="每周" checked>';
                    quen_str += '<input type="radio" name="quen" lay-filter="quen" value="m" title="每月">';
                    $("#quenweekDiv").removeClass('layui-hide');
                    $("#quendateDiv").addClass('layui-hide');
                }
                if(data.quen == 'm'){
                    quen_str += '<input type="radio" name="quen" lay-filter="quen" value="d" title="每日">';
                    quen_str += '<input type="radio" name="quen" lay-filter="quen" value="w" title="每周">';
                    quen_str += '<input type="radio" name="quen" lay-filter="quen" value="m" title="每月" checked>';
                    $("#quenweekDiv").addClass('layui-hide');
                    $("#quendateDiv").removeClass('layui-hide');
                }
                $('#quen').html(quen_str);



                var weekArr = [
                    {name:'周一',value:1},
                    {name:'周二',value:2},
                    {name:'周三',value:3},
                    {name:'周四',value:4},
                    {name:'周五',value:5},
                    {name:'周六',value:6},
                    {name:'周日',value:7},
                ];
                var quenweek_str = '';
                for (var key in weekArr){
                    if(str_in_array(weekArr[key].value, data.quenweek.split(","))){
                        quenweek_str += '<input type="checkbox" name="quenweek[]" lay-skin="primary" value="'+weekArr[key].value+'" title="'+weekArr[key].name+'" checked>';
                    }else{
                        quenweek_str += '<input type="checkbox" name="quenweek[]" lay-skin="primary" value="'+weekArr[key].value+'" title="'+weekArr[key].name+'">';
                    }
                }
                $('#quenweek').html(quenweek_str);


                var quendate_str = '';
                for (var i=1; i<=31; i++){
                    if(str_in_array(i, data.quendate.split(","))){
                        quendate_str += '<input type="checkbox" name="quendate[]" lay-skin="primary" value="'+i+'" title="'+i+'" checked>';
                    }else{
                        quendate_str += '<input type="checkbox" name="quendate[]" lay-skin="primary" value="'+i+'" title="'+i+'">';
                    }
                }
                $('#quendate').html(quendate_str);


                var duitemp = res.data.duitemp;
                var duier_str = '';
                for (var key in duitemp){
                    if(str_in_array(key, data.duitemp.split(","))){
                        if(str_in_array(key, data.duier.split(","))){
                            duier_str += '<input type="checkbox" name="duier[]" lay-skin="primary" lay-filter="duier[]" value="'+key+'" title="'+duitemp[key]+'" checked>';
                        }else{
                            duier_str += '<input type="checkbox" name="duier[]" lay-skin="primary" lay-filter="duier[]" value="'+key+'" title="'+duitemp[key]+'">';
                        }
                    }
                }
                $('#duitemp').html(duier_str);


                $("#zhier").html('');
                if(str_in_array('pointer', data.duier.split(","))){
                    $("#duierDiv").removeClass('layui-hide');
                    if(data.zhier_info.length > 0){
                        tan_tags_init('#zhier', data.zhier_info);
                    }
                }

                if(data.state > 0){
                    $("#state").prop('checked',true);
                }else{
                    $("#state").prop("checked",false);
                }


                render_from();
            }
        });
    }

</script>